<template>
  <div>
    <el-row :gutter="20" class="app-container">
      <!-- 功能按钮 -->
      <el-col :span="24" :xs="24">
        <el-col :span="5">
          <el-form>
            <el-form-item label-width="0" style="padding: 0;margin: 0;">
              <el-input
                v-model="diseCatalogParams.name"
                placeholder="请输入分类别名"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
              />
            </el-form-item>
          </el-form>
          <el-col>
            <el-col :span="12">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleCatalogAdd"
                v-hasPermi="['dms:diseCatalog:add']"
              >新增
              </el-button>
            </el-col>
            <el-col :span="12">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                :disabled="catalogMultiple"
                @click="handleCatalogDelete"
                v-hasPermi="['dms:diseCatalog:remove']"
              >删除</el-button>
            </el-col>
          </el-col>
        </el-col>
        <el-col :span="19">
          <!-- 条件搜索 + 目录显示 刷新等 -->
          <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="目录id" prop="catId" v-if="show">
              <el-input
                v-model="queryParams.catId"
                placeholder="请输入目录id"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="疾病编码" prop="code">
              <el-input
                v-model="queryParams.code"
                placeholder="请输入疾病编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="疾病名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入疾病名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="ICD编码" prop="icd">
              <el-input
                v-model="queryParams.icd"
                placeholder="请输入ICD编码"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!--<el-form-item label="状态" prop="status" v-if="show">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                <el-option label="请选择字典生成" value=""/>
              </el-select>
            </el-form-item>-->
            <el-form-item>
              <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
          <!-- 功能按钮 -->
          <el-row :gutter="10">
            <el-col :span="13">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['dms:dise:add']"
                >新增
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['dms:dise:edit']"
                >修改
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['dms:dise:remove']"
                >删除
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['dms:dise:export']"
                >导出
                </el-button>
              </el-col>
            </el-col>
            <el-col :span="11">
              <div class="top-right-btn">
                <el-col :span="19">
                  <el-input v-model="diseCatalogName" size="small" disabled placeholder="请选择当前选择目录">
                    <template slot="prepend">当前选择目录</template>
                  </el-input>
                </el-col>
                <el-col :span="5">
                  <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                    <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
                  </el-tooltip>
                  <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
                    <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
                  </el-tooltip>
                </el-col>
              </div>
            </el-col>
          </el-row>
        </el-col>
      </el-col>

      <!-- 数据显示 -->
      <el-col :xs="24">
        <el-col :span="5" class="app-container">
          <div class="head-container" style="height: 442px">
            <el-table v-loading="loading"  :data="diseCatalogList" style=" width: 100%;"
                      @row-click="handleCatalogRowClick"
                      @selection-change="handleCatalogSelectionChange">
              <el-table-column type="selection"  align="center"/>
              <el-table-column prop="id" label="id" h align="center" v-if="show">
              </el-table-column>
              <el-table-column prop="name" label="疾病分类目录" style="height: 55px" align="center">
              </el-table-column>
            </el-table>
          </div>
          <div style="margin-top: 30px">
            <el-col :span="7">
              <el-tag>
                共{{Math.floor((diseCatalogTotal+diseCatalogParams.pageSize-1)/diseCatalogParams.pageSize)}}页
              </el-tag>
            </el-col>
            <el-col :span="15" style="padding-left: 0; padding-right: 10px;">
              <el-pagination
                v-show="diseCatalogTotal>0"
                @current-change="getDiseCatalogList"
                :current-page.sync="diseCatalogParams.pageNum"
                :page-size.sync="diseCatalogParams.pageSize"
                layout="prev,next,jumper"
                :total="diseCatalogTotal"
              >
              </el-pagination>
            </el-col>
          </div>
        </el-col>
        <el-col :span="19" class="app-container">
          <!--表格-->
          <div style="height: 442px">
            <el-table v-loading="loading" :data="diseList"
                      @selection-change="handleSelectionChange" max-height="442px" style="width: 100%;">
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="id" align="center" prop="id" v-if="show"/>
              <el-table-column label="目录id" align="center" prop="catId" v-if="show"/>
              <el-table-column label="疾病编码" align="center" prop="code"/>
              <el-table-column label="疾病名称" align="center" prop="name"/>
              <el-table-column label="ICD编码" align="center" prop="icd"/>
              <el-table-column label="状态" align="center" prop="status" v-if="show"/>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['dms:dise:edit']"
                  >修改
                  </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['dms:dise:remove']"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-col>
    </el-row>


    <!-- 添加或修改诊断类型(疾病)管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" @opened="dialogOpened" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="目录id" prop="catId" v-if="show">
          <el-input v-model="form.catId" placeholder="请输入目录id"/>
        </el-form-item>
        <el-form-item label="疾病编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入疾病编码"/>
        </el-form-item>
        <el-form-item label="疾病名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入疾病名称"/>
        </el-form-item>
        <el-form-item label="ICD编码" prop="icd">
          <el-input v-model="form.icd" placeholder="请输入ICD编码"/>
        </el-form-item>
        <!--<el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改诊断类型（疾病）目录对话框 -->
    <el-dialog :title="catalogTitle" :visible.sync="catalogOpen" width="550px" append-to-body>
      <el-form ref="catalogForm" :model="catalogForm" :rules="catalogRules" label-width="100px">
        <el-form-item label="疾病目录名" prop="name">
          <el-input v-model="catalogForm.name" placeholder="请输入疾病目录名"/>
        </el-form-item>
        <!--  <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio label="1">请选择字典生成</el-radio>
            </el-radio-group>
          </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCatalogForm">确 定</el-button>
        <el-button @click="catalogCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {listDise, getDise, delDise, addDise, updateDise, exportDise} from "@/api/dms/dise"
  import {
    listDiseCatalog,
    getDiseCatalog,
    delDiseCatalog,
    addDiseCatalog,
    updateDiseCatalog,
    exportDiseCatalog
  } from "@/api/dms/diseCatalog";

  export default {
    name: "Dise",
    data() {
      return {
        // 设置隐藏列
        show: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 选中数组
        catalogIds: [],
        // 非单个禁用
        catalogSingle: true,
        // 非多个禁用
        catalogMultiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 诊断类型(疾病)管理表格数据
        // 目录总条数
        diseCatalogTotal: 0,
        diseList: [],
        // 诊断类型(疾病)目录表格数据
        diseCatalogList: [],
        // 疾病目录名称
        name: undefined,
        // 弹出层标题
        title: "",
        // 弹出层标题
        catalogTitle: "",
        // 是否显示弹出层
        open: false,
        // 是否目录显示弹出层
        catalogOpen: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          catId: null,
          code: null,
          name: null,
          icd: null,
          status: null
        },
        // diseCatalog查询参数
        diseCatalogParams: {
          id: undefined,
          pageNum: 1,
          pageSize: 10,
          name: undefined,
        },
        diseCatalogName: undefined,
        diseCatalogId: undefined,
        // 表单参数
        form: {},
        // 目录表单参数
        catalogForm: {},
        // 表单校验
        catalogRules: {
          name: [{
            required: true, message: "疾病目录名称不能为空", trigger: 'change'
          }],
        },
        rules:{
          code: [{
            required: true, message: "疾病编码不能为空", trigger: 'change'
          }],
          name: [{
            required: true, message: "疾病名称不能为空", trigger: 'change'
          }],
          icd: [{
            required: true, message: "疾ICD编码不能为空", trigger: 'change'
          }],
        }
      };
    },
    watch: {
      // 根据名称筛选目录
      'diseCatalogParams.name'(val) {
        this.getDiseCatalogList();
      }
    },
    created() {
      //  this.getList();
      this.getDiseCatalogList();
    },
    methods: {
      /** 查询诊断类型(疾病)管理列表 */
      getList() {
        this.loading = true;
        listDise(this.queryParams).then(response => {
          this.diseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 查询诊断类型(疾病)目录列表 */
      getDiseCatalogList() {
        this.loading = true;
        listDiseCatalog(this.diseCatalogParams).then(response => {
          this.diseCatalogList = response.rows;
          this.diseCatalogTotal = response.total;
          this.loading = false;
        });
      },
      /** 目录表 点击行事件*/
      handleCatalogRowClick(data) {
        this.queryParams.catId = data.id;  //获得目录IdD
        this.diseCatalogName = data.name;    //获得目录名称
        this.getList();
      },
      /**
       * 对话框打开回调
       * */
      dialogOpened: function () {
        // 将目录id 加入
        this.form.catId = this.queryParams.catId;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 目录对话框取消按钮
      catalogCancel() {
        this.catalogOpen = false;
        this.catalogReset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          catId: null,
          code: null,
          name: null,
          icd: null,
          status: 0
        };
        this.resetForm("form");
      },
      // 目录表单重置
      catalogReset() {
        this.catalogForm = {
          name: null
        };
        this.resetForm("catalogForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      // 目录多选框选中数据
      handleCatalogSelectionChange(selection) {
        this.catalogIds = selection.map(item => item.id)
        this.catalogSingle = selection.length !== 1
        this.catalogMultiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加诊断类型(疾病)管理";
      },
      /** 目录新增按钮操作 */
      handleCatalogAdd() {
        this.catalogReset();
        this.catalogOpen = true;
        this.catalogTitle = "添加诊断类型(疾病)目录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDise(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改诊断类型(疾病)管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if(this.form.catId===null||this.form.catId===undefined||this.form.catId===''){
            this.$notify({
              title: '警告',
              message: '请选择一个目录，然后进行添加',
              type: 'warning'
            });
            return;
          }
          if (valid) {
            if (this.form.id != null) {
              updateDise(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addDise(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },

      /** 目录表单提交按钮 */
      submitCatalogForm() {
        this.$refs["catalogForm"].validate(valid => {
          if (valid) {
            addDiseCatalog(this.catalogForm).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getDiseCatalogList();
              }
            });
          }
        });
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除诊断类型(疾病)管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delDise(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },

      /** 目录删除按钮操作 */
      handleCatalogDelete(row) {
        const ids = row.id || this.catalogIds;
        this.$confirm('是否确认删除诊断类型（疾病）目录编号为"' + ids + '"的数据项,<span style="color: red">注意包括目录下的疾病</span>?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          dangerouslyUseHTMLString: true   // 设置会识别标签
        }).then(function() {
          return delDiseCatalog(ids);
        }).then(() => {
          this.getDiseCatalogList();
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有诊断类型(疾病)管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportDise(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };

</script>
<style scope>
  .el-table__body tr,
  .el-table__body td {
    padding: 0;
    height: 40px;
  }

  .el-pagination .btn-prev {
    padding: 0;
  }
  .el-pagination .btn-next {
     padding:0;
  }
  .el-pagination__jump {
     margin-left: 0px
  }
</style>
